import requests
from lxml import etree
from tqdm import tqdm
from threading import Thread
import re
import librosa
import os
import threading

links=[]
mlist = []
lis=[]
headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36'
}
rootpath="C:/Users/User/Desktop/code/pycode/lab12/MP3/"
# 创建同步锁对象
lock = threading.Lock()

class LinkAddressress(Thread):
    def __init__(self,i,j):
        super().__init__()
        self.i=i
        self.j=j
    def run(self):
        global links
        lock.acquire()
        try:
            for i in tqdm(range(self.i, self.j)):
                url = f'https://www.51voa.com/VOA_Standard_{i}.html'#翻页
                response = requests.get(url)
                html = etree.HTML(response.text)
                for j in range(0,50):
                    links = links + html.xpath('//*[@id="righter"]/div[3]/ul/li[%s]/a/@href'%str(j))
            print(len(links),links)
        finally:
            lock.release()

class Mp3Link(Thread):
    def __init__(self,i,j):
        super().__init__()
        self.i=i
        self.j=j
    
    def run(self):
        global mlist
        lock.acquire()
        try:
            headers = {
                'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36'
            }
            for link in tqdm(links):
                url = 'https://www.51voa.com'+link
                # print(url)
                response = requests.get(url,headers = headers)
                mlist = mlist+list(set(re.findall(r'https://.+?\.mp3', response.text)))
            print(len(mlist),mlist[:10])
        finally:
            lock.release()

class SaveMp3(Thread):
    def __init__(self,i,j):
        super().__init__()
        self.i=i
        self.j=j
    def run(self):
        #下载mp3文件
        for murl in tqdm(mlist):
            #print(murl)
            mp3_stream = requests.get(murl,headers = headers).content
            fname = murl[murl.rfind('/')+1:]
            print(fname)
            fname="C:/Users/User/Desktop/code/pycode/lab12/MP3/"+fname
            with open(fname,'wb') as f:
                f.write(mp3_stream)

def Mp3lis(rootpath):
    lis=[]
    # 遍历根目录下的所有文件和子文件夹
    for root, dirs, files in os.walk(rootpath):
        # 遍历当前文件夹下的所有文件
        for file in files:
            # 输出文件所在路径及文件名
            lis.append(os.path.join(root, file))
    return lis

class Speed(Thread):
    def __init__(self,lis):
        super().__init__()
        self.lis=lis
    def run(self):
        def speechrate(y,sr):
            onsets = librosa.onset.onset_detect(y=y,sr=sr,units="time",hop_length=128,backtrack=False) 
            number_of_words = len(onsets)
            print(number_of_words)
            duration = len(y)/sr
            print(duration)
            words_per_second = number_of_words/duration
            print(f'words-per-second: {words_per_second}\nduration: {duration} seconds\nnumber-of-words: {number_of_words}')
            return words_per_second
        for i in range(len(self.lis)):
            filename = self.lis[i]
            y, sr = librosa.load(filename, sr=None)
            print(len(y))
            speechrate(y, sr)
            print('done')

            
if __name__ == '__main__':
    #获取新的链接地址列表
    address=LinkAddressress(1,2)
    address.start()
    address.join()
    #获取 MP3 地址
    mp3=Mp3Link(1,2)
    mp3.start()
    mp3.join()
    #保存 MP3 文件
    sa=SaveMp3(1,2)
    sa.start()
    sa.join()
    #对 MP3 进行计算语速
    spe=Speed(Mp3lis(rootpath)[0:len(Mp3lis(rootpath))//2])
    spe.start()
    spe1=Speed(Mp3lis(rootpath)[len(Mp3lis(rootpath))//2:len(Mp3lis(rootpath))])
    spe1.start()
    spe.join()
    spe1.join()